package com.jmebananas.input.action;

import com.jme.input.MouseInput;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyStrafeDownAction;
import com.jme.input.action.KeyStrafeLeftAction;
import com.jme.input.action.MouseInputAction;
import com.jme.renderer.Camera;

/**
 * MousePan
 * 
 * @author Glenn Powell
 * 
 */
public class MouseBorderAction extends MouseInputAction {

	private KeyStrafeDownAction keyDown;

	private KeyStrafeLeftAction keyLeft;

	private InputActionEvent event;

	private Camera camera;
	
	private int left;
	
	private int top;
	
	private int right;
	
	private int bottom;
	
	private int border;


	public MouseBorderAction(Camera camera, float speed, int left, int top, int right, int bottom, int border) {
		this.camera = camera;
		this.speed = speed;
		this.left = left;
		this.top = top;
		this.right = right;
		this.bottom = bottom;
		this.border = border;

		keyDown = new KeyStrafeDownAction(camera, speed);
		keyLeft = new KeyStrafeLeftAction(camera, speed);

		event = new InputActionEvent();
	}

	public void setSpeed(float speed) {
		super.setSpeed(speed);
		keyDown.setSpeed(speed);
		keyLeft.setSpeed(speed);
	}

	public void performAction(InputActionEvent evt) {
		if (camera.isParallelProjection()) {
			int mouseX = MouseInput.get().getXAbsolute();
			int mouseY = MouseInput.get().getYAbsolute();
			if (border > 0) {
				if (mouseX < left + border) {
					event.setTime((left + border - mouseX) / (float)border);
					keyLeft.performAction(event);
				} else if (mouseX >= right - border) {
					event.setTime((right - border - mouseX) / (float)border);
					keyLeft.performAction(event);
				}
				if (mouseY < top + border) {
					event.setTime((mouseY - top - border) / (float)border);
					keyDown.performAction(event);
				} else if (mouseY >= bottom - border) {
					event.setTime((border + mouseY - bottom) / (float)border);
					keyDown.performAction(event);
				}
			}
		}
	}
	
	public int getBorder() {
		return border;
	}
	
	public void setBorder(int border) {
		this.border = border;
	}
	
}